//
//  ViewController.m
//  UIViewTransformPoint
//
//  Created by codew on 4/30/21.
//

#import "ViewController.h"


@interface ViewController ()



@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    
}



- (void)doSomething
{
    NSLog(@"2");
}






/** 死锁 */
- (void)testGCD1
{
    dispatch_sync(dispatch_get_main_queue(), ^{
        
        [self doSomething];
    });
    
    
    
}




/** 同步串行没有问题 */

- (void)testGCD2
{
    //
    dispatch_queue_t queue = dispatch_queue_create("com.itopengl", DISPATCH_QUEUE_SERIAL);
//    dispatch_queue_t queue = dispatch_queue_create("com.itopengl", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(queue, ^{
        
        [self doSomething];
    });
    NSLog(@"===++++=======");
}


/**
 结果: 12345
 */
- (void)testGCD3
{
    NSLog(@"1");
    NSLog(@"%@", [NSThread currentThread]);
    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        
        
        NSLog(@"2");
        NSLog(@"%@", [NSThread currentThread]);
        dispatch_sync(dispatch_get_global_queue(0, 0), ^{
            
           
            
            NSLog(@"3");
            NSLog(@"%@", [NSThread currentThread]);
        });
        
        NSLog(@"4");
        NSLog(@"%@", [NSThread currentThread]);
        
    });
    
    NSLog(@"5");
    NSLog(@"%@", [NSThread currentThread]);
}


/**
 结果: 13
 
 异步+全局队列,这种方式, 实际上是在GCD底层所维护的线程池, 中的某一个线程中处理
 GCD下面的这些线程, 默认是没有开启runloop的
 performSelector: withObject: afterDelay:这个方法, 要创建提交任务到runloop的逻辑
 然后 GCD下面的这些线程没有runloop, 所以performSelector: withObject: afterDelay:这个方法就无效了
 performSelector: withObject: afterDelay:这个方法要想有效, 必须满足所处的线程有runloop. 没有的话就失效

 */
- (void)testGCD4
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        NSLog(@"1");
        
        [self performSelector:@selector(doSomething) withObject:nil afterDelay:0];
        
        NSLog(@"3");
    });
    
}


@end
